﻿#include "appsettingdialog.h"
#include "ui_appsettingdialog.h"
#include "flatui.h"
#include <QSettings>
#include <QDebug>
#include <QMessageBox>
#include <QJsonObject>
#include <QJsonDocument>
#include <QProcess>
#include <globdataobj.h>
#include <QSqlQuery>
#include "nettools.h"

AppSettingDialog::AppSettingDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AppSettingDialog)
{
    ui->setupUi(this);

    //窗口关闭时释放内存
    this->setAttribute(Qt::WA_DeleteOnClose);
    QSettings settings;
    //根据设置勾选复选框
    if(settings.value("app/Tract").isValid() && settings.value("app/Tract").toString()=="Y"){
        ui->tract_checkBox->setChecked(true);
    }
    if(settings.value("app/AutoLock").isValid() && settings.value("app/AutoLock").toString()=="Y"){
        ui->autoLock_checkBox->setChecked(true);
        ui->autoLock_spinBox->setEnabled(true);
    }else{
        ui->autoLock_spinBox->setEnabled(false);
    }
    //根据设置勾选是否启用代理复选框
    if(settings.value("network/proxy/enable").isValid() && settings.value("network/proxy/enable").toString()=="Y"){
        ui->checkBoxProxyEnable->setChecked(true);
    }
    if(settings.value("cloud/activation").isValid()&&settings.value("cloud/activation").toString()=="Y"){
        //已激活云服务
        if(settings.value("cloud/account").isValid()){
            //隐藏绑定的widget,显示用于显示已绑定的widget
            ui->widget_3->hide();
            ui->labelAccount->setText("<html><head/><body><p>已绑定账号:"
                                      "<span style=\" font-size:11pt; font-weight:600; color:#2f8d00;\">"
                                      ""+settings.value("cloud/account").toString()+"</span></p></body></html>");
            ui->labelBindedServerAddr->setText("<p>服务器地址:<span style='color:green;font-size:11pt;font-weight:600;'>"+settings.value("cloud/serverAddr").toString()
                                               +"</span></p>");

        }else{
            ui->widget_5->hide();
        }
    }else{
        ui->widget_5->hide();
    }
    //关闭窗口时最小化到托盘还是直接退出
    connect(ui->tract_checkBox,&QCheckBox::stateChanged,[=](){
        QSettings settingst;
        if(ui->tract_checkBox->isChecked()){
            settingst.setValue("app/Tract",QString("Y"));

        }else{
            settingst.setValue("app/Tract",QString("N"));

        }
    });

    //是否启动自动锁定
    connect(ui->autoLock_checkBox,&QCheckBox::stateChanged,[=](){
       QSettings settingst;
       if(ui->autoLock_checkBox->isChecked()){
           settingst.setValue("app/AutoLock",QString("Y"));
           ui->autoLock_spinBox->setEnabled(true);
       }else{
           settingst.setValue("app/AutoLock",QString("N"));
           ui->autoLock_spinBox->setEnabled(false);
       }

    });
    connect(ui->autoLock_spinBox,SIGNAL(valueChanged(int)),this,SLOT(autoLockSpinBoxValueChanged(int)));
    //是否启用代理
    connect(ui->checkBoxProxyEnable,&QCheckBox::stateChanged,[=](){
        QSettings settingsp;
        QNetworkProxy proxy;
        proxy.setType(QNetworkProxy::HttpProxy);
        QString server = ui->lineEditProxyServer->text().trimmed();
        int port = ui->lineEditProxyPort->text().trimmed().toInt();
        QString user = ui->lineEditProxyUsername->text().trimmed();
        QString pass = ui->lineEditProxyPassword->text();
        if(server.isEmpty()){
            ui->labelProxyMsg->setText("<span style='color:red;'>服务器地址不能为空！</span>");
            return;
        }
        if(port<=0){
            ui->labelProxyMsg->setText("<span style='color:red;'>服务器端口填写错误！</span>");
            return;
        }

        if(ui->checkBoxProxyEnable->isChecked()){

            proxy.setHostName(server);
            proxy.setPort(port);

            settingsp.setValue("network/proxy/server",server);
            settingsp.setValue("network/proxy/port",port);

            if(!user.isEmpty()){
                proxy.setUser(user);
                settingsp.setValue("network/proxy/user",user);
            }
            if(!pass.isEmpty()){
                proxy.setPassword(pass);
                settingsp.setValue("network/proxy/pass",pass);
            }
            QNetworkProxy::setApplicationProxy(proxy);

            settingsp.setValue("network/proxy/enable","Y");
            ui->labelProxyMsg->setText("<span style='color:green;'>代理已启用</span>");
        }else{
            QNetworkProxy cproxy;
            QNetworkProxy::setApplicationProxy(cproxy);
            settingsp.setValue("network/proxy/enable","N");
            ui->labelProxyMsg->setText("<span style='color:green;'>代理已禁用</span>");
        }
    });

    //初始化网络标签页
    initNetworkSetting();

    initStyle();


}

AppSettingDialog::~AppSettingDialog()
{
    delete ui;
}

void AppSettingDialog::initStyle()
{

}

void AppSettingDialog::initNetworkSetting()
{
    QSettings settings;
    if(settings.value("network/proxy/server").isValid()&&!settings.value("network/proxy/server").toString().isEmpty()){
        ui->lineEditProxyServer->setText(settings.value("network/proxy/server").toString());
    }
    if(settings.value("network/proxy/port").isValid()&&!settings.value("network/proxy/port").toString().isEmpty()){
        ui->lineEditProxyPort->setText(settings.value("network/proxy/port").toString());
    }
    if(settings.value("network/proxy/user").isValid()&&!settings.value("network/proxy/user").toString().isEmpty()){
        ui->lineEditProxyUsername->setText(settings.value("network/proxy/user").toString());
    }
    if(settings.value("network/proxy/pass").isValid()&&!settings.value("network/proxy/pass").toString().isEmpty()){
        ui->lineEditProxyPassword->setText(settings.value("network/proxy/pass").toString());
    }
}

void AppSettingDialog::on_pushButton_clicked()
{
    QString account = ui->lineEditAccount->text().trimmed();
    QString server = ui->lineEditServarAddr->text().trimmed();
    if(account.isEmpty()){
        QMessageBox::warning(this,QString("提示"),QString("云账号不能为空!"));
        return;
    }
    if(server.isEmpty()){
        QMessageBox::warning(this,QString("提示"),QString("服务器地址不能为空!"));
        return;
    }

    QProcess p(0);
    p.start("wmic cpu get processorid");
    p.waitForStarted();
    p.waitForFinished();
    QString code;
    QStringList list = QString::fromLocal8Bit(p.readAllStandardOutput()).split("\r\r\n");
    if(list.count()>1){
        code = list.at(1);
    }
    code = code.trimmed();
    //发送请求
    //bindingAccount(account);
    NetTools *tools = new NetTools;
    connect(tools,SIGNAL(bindCloudAccountResult(bool,QString)),this,SLOT(bindCloudAccountResult(bool,QString)));
    tools->bindCloudAccount(account,server,code);

    ui->pushButton->setEnabled(false);
    ui->lineEditServarAddr->setEnabled(false);
}


//测试服务器是否可用
void AppSettingDialog::on_pushButton_2_clicked()
{
    QString server = ui->lineEditServarAddr->text().trimmed();
    if(server.isEmpty()){
        QMessageBox::critical(this,"错误","服务器地址不能为空！",QMessageBox::Ok,QMessageBox::Ok);
        return;
    }
    NetTools *net = new NetTools;
    connect(net,SIGNAL(testServerResult(bool,QString)),this,SLOT(testServerResult(bool,QString)));


    net->testServer(server);
}

void AppSettingDialog::on_pushButton_4_clicked()
{

    QSettings settings;

    QString server = ui->lineEditProxyServer->text().trimmed();
    int port = ui->lineEditProxyPort->text().trimmed().toInt();
    QString user = ui->lineEditProxyUsername->text().trimmed();
    QString pass = ui->lineEditProxyPassword->text();
    if(server.isEmpty()){
        ui->labelProxyMsg->setText("<span style='color:red;'>服务器地址不能为空！</span>");
        return;
    }
    if(port<=0){
        ui->labelProxyMsg->setText("<span style='color:red;'>服务器端口填写错误！</span>");
        return;
    }



    settings.setValue("network/proxy/server",server);
    settings.setValue("network/proxy/port",port);

    ui->labelProxyMsg->setText("<span style='color:green;'>代理设置成功！</span>");
    //if(settings.value("cloud/activation").isValid()&&settings.value("cloud/activation").toString()=="Y"){
}

void AppSettingDialog::testServerResult(bool b, QString s)
{
    if(b){
        ui->label_msg1->setText("<span style='color:green;'>服务器可用！</span>");
    }else{
        ui->label_msg1->setText("<span style='color:red;'>服务器不可用!"+s+"</span>");
    }
}

void AppSettingDialog::bindCloudAccountResult(bool b, QString s)
{
    if(b){
        ui->label_msg1->setText("<span style='color:green;'>绑定成功！</span>");
        QString server = ui->lineEditServarAddr->text().trimmed();
        QSettings settings;
        settings.setValue("cloud/activation","Y");
        settings.setValue("cloud/account",ui->lineEditAccount->text().trimmed());
        settings.setValue("cloud/serverAddr",server);

        //emit startSyncTask();
        QMessageBox::StandardButton reply;
        reply = QMessageBox::information(this,tr("提示"),tr("绑定云账号后需要重启程序！"),QMessageBox::Yes);
        if(reply == QMessageBox::Yes){
            QApplication *app;
            app->exit(773);
        }
    }else{
        ui->label_msg1->setText("<span style='color:red;'>绑定失败!"+s+"</span>");
        ui->pushButton->setEnabled(true);
    }
}

void AppSettingDialog::autoLockSpinBoxValueChanged(int v)
{
    QSettings settingst;
    settingst.setValue("app/AutoLockTime",v);
}
